class MarkerManager {
    constructor(map) {
        this._array = [];
        this._show = true;
        this._idque = 0;
        this._map = map;
    }

    //添加一个makrer
    addMarker(el, coordinates, offset, properties){
        let map = this._map;
        let marker = new mapboxgl.Marker(el);
        marker.setLngLat(coordinates);
        if(offset) marker.setOffset(offset);
        if(this._show){
            marker.addTo(map);
            marker.imagePointShow = true;
        }else{
            marker.imagePointShow = false;
        }
        this._array.push(marker);
        marker.id = "m_" + ++this._idque;
        marker.properties = properties || null;
        return marker;
    }

    getMarkers(){
        return this._array;
    }

    clear(){
        for(let i = 0 ; i < this._array.length; i++){
            let marker = this._array[i];
            marker.remove();
        }
        this._array = [];
    }

    hide(){
        for(let i = 0 ; i < this._array.length; i++){
            let marker = this._array[i];
            marker.remove();
            marker.show = false;
        }
        this._show = false;
    }

    show(){
        let map = this._map;
        for(let i = 0 ; i < this._array.length; i++){
            let marker = this._array[i];
            marker.addTo(map);
            marker.show = true;
        }
        this._show = true;
    }

    showMarker(markerId){
        if(!this._show) return;
        let map = this._map;
        for(let i = 0 ; i < this._array.length; i++){
            let marker = this._array[i];
            if(marker.show) continue;
            if(marker.id == markerId){
                marker.addTo(map);
                marker.show = true;
                return marker;
            }
        }
    }

    hideMarker(){
        if(!this._show) return;
        let map = this._map;
        for(let i = 0 ; i < this._array.length; i++){
            let marker = this._array[i];
            if(! marker.show) continue;
            if(marker.id == markerId){
                marker.remove();
                marker.show = false;
                return marker;
            }
        }
    }
}

export { MarkerManager }

export default MarkerManager